#pragma once

#include<iostream>
#include<stdarg.h>
#include<fcntl.h>
#include<time.h>
#include<unistd.h>

using namespace std;

#define SIZE 1024

#define Info 0
#define Debug 1
#define Warning 2
#define Error 3
#define Fatal 4

#define Screen 1
#define Onefile 2
#define Classfile 3

#define LogfileName "log.txt"

class Log
{
public:
    Log()
    {
        printMethod = Screen;
        path = "./log/";
    }
    ~Log()
    {}

    void  printOneFile(const string& logname,const string& logtxt)
    {
        string _logname = path + logname;
        int fd = open(_logname.c_str(),O_WRONLY | O_CREAT | O_APPEND,0666);
        if(fd < 0) return;
        write(fd,logtxt.c_str(),logtxt.size());
        close(fd);
    }
    void printClassFile(int level,const string& logtxt)
    {
        string filename = LogfileName;//默认文件名
        filename+=".";
        filename+=levelToString(level);
        //
        printOneFile(filename,logtxt);
    }

    void printLog(int level,const string& logtxt)
    {
        switch (printMethod)
        {
        case Screen:
        {
            cout<<logtxt<<endl;
            break;
        }
        case Onefile:
        {
            printOneFile(LogfileName,logtxt);
            break;
        }
        case Classfile:
        {
            printClassFile(level,logtxt);
            break;
        }
        default:
            break;
        }
    }

    //
    void printmethod(int method)
    {
        printMethod = method;
    }
   string levelToString(int level)
    {
        switch (level)
        {
            case Info: return "Info";
            case Debug: return "Debug";
            case Warning: return "Warning";
            case Error: return "Error";
            case Fatal: return "Fatal";
            default: return "None";
        }
    } 
    // void logmessage(int level,const char* format,...)
    // {
    //     char defaultbuffer[SIZE];
   
    //     //获取时间
    //     //time gettimeofday localtime(time_t* timep)->将time_t类型转换成struct tm结构体
    //     time_t t = time(nullptr);
    //     struct tm *ctime = localtime(&t);
    //     //把日志基本信息写到default中
    //     snprintf(defaultbuffer,sizeof(defaultbuffer),"[%s][%d-%d-%d %d:%d:%d] ",
    //     levelToString(level).c_str(),//string转成c形式
    //     1900+ctime->tm_year,1+ctime->tm_mon,ctime->tm_mday,
    //     ctime->tm_hour,ctime->tm_min,ctime->tm_sec);

    //     // cout<<time(nullptr)<<endl;
    //     // cout << 1900+ctime->tm_year << "-"<< 1+ctime->tm_mon << "-" << ctime->tm_mday<<" ";
    //     // cout<<ctime->tm_hour<<":"<<ctime->tm_min<<":"<<ctime->tm_sec<<endl;

    //     //可变部分
    //     va_list s;
    //     va_start(s,format);
    //     char userbuffer[SIZE];
    //     vsnprintf(userbuffer,sizeof(userbuffer),format,s);
    //     va_end(s);

    //     char logtxt[SIZE*2];
    //     int n = snprintf(logtxt,sizeof(logtxt),"%s %s",defaultbuffer,userbuffer);
    //     logtxt[n] = '\n';
    //     // printf("%s\n",logtxt);//暂时打印-显示器打印
    //     printLog(level,logtxt);
    //     //每次有日志，追加写入

    // }
    void operator()(int level,const char* format,...)
    {
         char defaultbuffer[SIZE];
   
        //获取时间
        //time gettimeofday localtime(time_t* timep)->将time_t类型转换成struct tm结构体
        time_t t = time(nullptr);
        struct tm *ctime = localtime(&t);
        //把日志基本信息写到default中
        snprintf(defaultbuffer,sizeof(defaultbuffer),"[%s][%d-%d-%d %d:%d:%d] ",
        levelToString(level).c_str(),//string转成c形式
        1900+ctime->tm_year,1+ctime->tm_mon,ctime->tm_mday,
        ctime->tm_hour,ctime->tm_min,ctime->tm_sec);

        // cout<<time(nullptr)<<endl;
        // cout << 1900+ctime->tm_year << "-"<< 1+ctime->tm_mon << "-" << ctime->tm_mday<<" ";
        // cout<<ctime->tm_hour<<":"<<ctime->tm_min<<":"<<ctime->tm_sec<<endl;

        //可变部分
        va_list s;
        va_start(s,format);
        char userbuffer[SIZE];
        vsnprintf(userbuffer,sizeof(userbuffer),format,s);
        va_end(s);

        char logtxt[SIZE*2];
        int n = snprintf(logtxt,sizeof(logtxt),"%s %s",defaultbuffer,userbuffer);
        logtxt[n] = '\n';
        // printf("%s\n",logtxt);//暂时打印-显示器打印
        printLog(level,logtxt);
        //每次有日志，追加写入
    }

private:
    int printMethod;
    string path;
};

